import {defineStore} from "pinia";
import api from '@/api';
import router from '@/router';
import utils from "@/utils";

interface Store {
  apiVerson: string;
  unread: number;
  applyMap: any;
  auditMap: any;
  historyMap: any[];
}

export const useMainStore = defineStore("mainStore", {
  state: (): Store => ({
    apiVerson: "",
    unread: 0,
    applyMap: {
      0: "/backendSystem/dailymanager/leave",
      1: "/backendSystem/dailymanager/travel",
      2: "/backendSystem/dailymanager/seal",
      3: "/backendSystem/dailymanager/contract",
      4: "/backendSystem/dailymanager/expense",
      5: "/backendSystem/dailymanager/backup",
      6: "/backendSystem/dailymanager/budget",
      7: '/backendSystem/summarymanager/leave',
    },
    auditMap: {
      0: "/backendSystem/auditmanager/leave",
      1: "/backendSystem/auditmanager/travel",
      2: "/backendSystem/auditmanager/seal",
      3: "/backendSystem/auditmanager/contract",
      4: "/backendSystem/auditmanager/expense",
      5: "/backendSystem/auditmanager/backup",
      6: "/backendSystem/auditmanager/budget",
    },
    historyMap: utils.getStorage('historyMap', []),
  }),
  actions: {
    flushUnread() {
      // api.MessageUnreadCount().then((res: any) => {
      //   if (!res.ResponseError) {
      //     this.unread = res.data.message;
      //   }
      // });
    },
    jumpPath(status: number, types: number, bill: number) {
      let path = (status === 0 ? this.applyMap[types] : this.auditMap[types]) + '?openbill=' + bill;
      router.push(path);
    },
    setHistoryMap(item: any) {
      this.historyMap = this.historyMap.filter((i: any) => i[2] !== item[2]);
      this.historyMap.unshift(item);
      utils.setStorage('historyMap', this.historyMap)
    },
    clearHistoryMap() {
      this.historyMap = [];
      utils.setStorage('historyMap', this.historyMap)
    }
  },
});
